package com.shigu.cron;

import com.opentae.data.mall.beans.*;
import com.opentae.data.mall.interfaces.*;
import com.opentae.data.mall.pojo.BonusReportVo;
import com.opentae.data.mall.pojo.TradeReportData;
import com.opentae.data.mall.pojo.VipReportVo;
import com.opentae.data.mall.pojo.WalletSum;
import com.shigu.cron.vo.DailyTradeData;
import com.shigu.trade.enums.PayWay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/*
 * create by  过儿  2019-11-20
 * 金钱记录报表
 */
@Component
public class MoneyReportTask {
    @Autowired
    AdditionMoneyMapper additionMoneyMapper;
    @Autowired
    MallWalletMapper mallWalletMapper;
    @Autowired
    CashApplyMapper cashApplyMapper;
    @Autowired
    MallTradeMapper mallTradeMapper;
    @Autowired
    PayWxApplyMapper payWxApplyMapper;
    @Autowired
    DailyMoneyReportMapper dailyMoneyReportMapper;
    @Autowired
    MallBonusMapper mallBonusMapper;
    @Autowired
    VipBonusMapper vipBonusMapper;
    @Autowired
    VipReportMapper vipReportMapper;
    @Autowired
    SaleBonusReportMapper saleBonusReportMapper;
    @Autowired
    TradeReportMapper tradeReportMapper;

    private static final SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
    private static final SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");

    @Scheduled(cron = "0 0 0 * * ?")
    public void reportTask() {
        //获取前一天的时间
        String date = getLastDay();
        try {
            productDailyReport(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * @description 统计数据
     * @author 郭尔
     * @date 2019-11-21 9:48
     */
    public void productDailyReport(String date) throws ParseException {
        Date start = sdf2.parse(date + " 00:00:00");
        Date end = sdf2.parse(date + " 23:59:59");

        DailyMoneyReport report = new DailyMoneyReport();
        report.setDate(date);
        //先插入
        dailyMoneyReportMapper.insertSelective(report);
        //获取所有用户账户总余额和抵用券余额
        WalletSum walletData = mallWalletMapper.getWalletMoney();
        //微信支付成功的交易量
        Double income = payWxApplyMapper.wxDailyMoney(start, end);
        //已提现的金额
        Double cashed = cashApplyMapper.beCashedMoney(start, end);
        //提现审核中的金额(无需限制时间)
        Double freezeMoney = cashApplyMapper.freezeMoney();
        //订单vip奖励金额,今日发货的订单的奖励，和未发货的订单的所含奖励
        SaleBonusReport saleBonusReport = saleBonusReport(report.getReportId(), date, start, end);
        //vip购买奖励金额
        Double vipTotal = vipReport(report.getReportId(), date, start, end);
        // 额外添加余额和抵用券
        Double walletMoney = additionMoneyMapper.getDailyAddition(1, start, end);//余额
        Double walletVoucher = additionMoneyMapper.getDailyAddition(2, start, end);//抵用券
        //订单报告
        DailyTradeData dailyTradeData = tradeReport(report.getReportId(), date, start, end);


        report.setTotalWalletMoney(walletData.getTotalMoney());
        report.setTotalVoucher(walletData.getTotalVoucher());
        report.setWxPay(income.toString());
        report.setWxPay(income.toString());
        report.setCashMoney(cashed.toString());
        report.setWaitCash(freezeMoney.toString());
        report.setVipTotalBonus(vipTotal.toString());
        report.setSaleSendlBonus(saleBonusReport.getSaleSendBonus());
        report.setSaleWaitBonus(saleBonusReport.getSaleWaitBonus());
        report.setTradeMoney(dailyTradeData.getTradeTotal().toString());
        report.setTradePostfee(dailyTradeData.getPostfee().toString());
        report.setAdditionWallet(walletMoney.toString());
        report.setAdditionVoucher(walletVoucher.toString());

        dailyMoneyReportMapper.updateByPrimaryKeySelective(report);
    }


    private String getLastDay(){
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = (Date) calendar.getTime();
        return sdf1.format(date);
    }

    /**
     * @description 查出各个奖励细则，然后计算总和返回
     * @author 郭尔
     * @date 2019-11-21 10:36
     */
    private Double vipReport(Long reportId, String date, Date start, Date end) {
        VipReport vipReport = new VipReport();
        vipReport.setDate(date);
        vipReport.setReportId(reportId);

        VipReportVo vo = vipBonusMapper.getVipReportData(start, end);

        vipReport.setVipAgentBonus(vo.getVipAgentBonus().toString());
        vipReport.setVipOwnerBonus(vo.getVipOwnerBonus().toString());
        vipReport.setVipBossBonus(vo.getVipBossBonus().toString());
        vipReport.setLvOwnerBonus(vo.getLvOwnerBonus().toString());
        vipReport.setLvBossBonus(vo.getLvBossBonus().toString());

        Double total = vo.getVipAgentBonus() + vo.getVipOwnerBonus() + vo.getVipBossBonus() + vo.getLvOwnerBonus() + vo.getLvBossBonus();
        vipReport.setTotalBonus(total.toString());
        vipReport.setVipTotalBonus(total.toString());
        vipReportMapper.insertSelective(vipReport);

        return total;
    }

    /**
     * @description 订单的奖励
     * @author 郭尔
     * @date 2019-11-21 12:59
     */
    private SaleBonusReport saleBonusReport(Long reportId, String date, Date start, Date end) {
        //查询今日发货的订单奖励
        BonusReportVo data1 = mallBonusMapper.selBonusData(start, end, 1);
        BonusReportVo data2 = mallBonusMapper.selBonusData(start, end, 2);

        SaleBonusReport report = new SaleBonusReport();
        report.setReportId(reportId);
        report.setDate(date);
        report.setSaleBoss(data1.getBossBonus().toString());
        report.setSaleOwner(data1.getOwnerBonus().toString());
        report.setSaleStaff(data1.getStaffBonus().toString());
        double sendTotal = data1.getBossBonus() + data1.getOwnerBonus() + data1.getStaffBonus();
        report.setSaleSendBonus(Double.toString(sendTotal));


        report.setWaitBoss(data2.getBossBonus().toString());
        report.setWaitOwner(data2.getOwnerBonus().toString());
        report.setWaitStaff(data2.getOwnerBonus().toString());
        double waitTotal = data2.getBossBonus() + data2.getOwnerBonus() + data2.getStaffBonus();
        report.setSaleWaitBonus(Double.toString(waitTotal));

        //
        saleBonusReportMapper.insert(report);

        return report;
    }

    /**
     * @description 订单交易数据
     * @author 郭尔
     * @date 2019-11-21 14:56
     */
    private DailyTradeData tradeReport(Long reportId, String date, Date start, Date end) {

        TradeReport report = new TradeReport();
        report.setReportId(reportId);
        report.setDate(date);

        TradeReportData walletData = mallTradeMapper.selTradeReportData(start, end, PayWay.WALLET.getValue());
        TradeReportData wxData = mallTradeMapper.selTradeReportData(start, end, PayWay.WXPAY.getValue());
        TradeReportData voucherData = mallTradeMapper.selTradeReportData(start, end, PayWay.VOUCHER.getValue());

        report.setVoucherPayTrade(voucherData.getPayMoney().toString());
        report.setVoucherPayPostfee(voucherData.getPostfee().toString());
        report.setWalletPayTrade(walletData.getPayMoney().toString());
        report.setWalletPayPostfee(walletData.getPostfee().toString());
        report.setWxPayTrade(wxData.getPayMoney().toString());
        report.setWxPayPostfee(wxData.getPostfee().toString());

        double tradeMoney = walletData.getPayMoney() + voucherData.getPayMoney() + wxData.getPayMoney();
        double postfee = walletData.getPostfee() + voucherData.getPostfee() + wxData.getPostfee();

        tradeReportMapper.insert(report);

        return new DailyTradeData(tradeMoney, postfee);
    }
}
